package cn.iocoder.yudao.framework.datasource.config;

import cn.iocoder.yudao.framework.datasource.properties.TenantProperties;
import cn.iocoder.yudao.framework.mybatis.interceptor.TenantInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * TODO
 *
 * @className: TenantWebConfig
 * @author: tangkh
 * @date: 2023-09-19 17:33
 */
@ConditionalOnProperty(
        name = {"tenant.enabled"},
        havingValue = "true"
)
@Configuration
public class TenantWebConfig implements WebMvcConfigurer {

    @Autowired
    private TenantProperties tenantProperties;

    public TenantWebConfig() {
    }

    @Bean
    public TenantInterceptor tenantInterceptor() {
        return new TenantInterceptor(this.tenantProperties);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.tenantInterceptor()).addPathPatterns(new String[]{"/**"}).order(-9998);
    }
}
